﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wp.DAL;
using Wp.BLL;
using System.Web.UI.HtmlControls;

public partial class Modules_Posts_Controls_PostDetail : System.Web.UI.UserControl
{
    public string PostTextId { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        PostTextId = CommonHelper.QueryString("PostTextId", "");
        if (!IsPostBack)
        {
            LoadData();

        }
    }
    void LoadData()
    {
        TblPostsEntity item = TblPostsManager.CreateInstant().SelectOneByTextId(PostTextId, true);
        if (item != null)
        {
            List<TblPostsEntity> ls = new List<TblPostsEntity>();
            ls.Add(item);
            rpt.DataSource = ls;
            rpt.DataBind();
            LiteralTitle.Text = item.Title;
            SEO(item);
            LoadTag(item.Id);
            if (item.lsTermRelationships.Count > 0)
                LoadViewMore(item.CreatedDate, item.lsTermRelationships[0].Slug);
            TblPostMetaEntity imeta = TblPostMetaManager.CreateInstant().SelectOne(item.Id, "NumView");
            if (imeta == null)
            {
                imeta = new TblPostMetaEntity();
                imeta.Id = -1;
                imeta.PostId = item.Id;
                imeta.MetaKey = "NumView";
                imeta.MetaValue = "1";
            }
            else
            {
                imeta.MetaValue = (imeta.MetaValue.ToInt() + 1).ToString();
            }
            TblPostMetaManager.CreateInstant().InsertUpdate(imeta);
            //LoadComment(item.Id, -1, "approved");
            LoadComment(item.Id, -1, "approved");
        }
    }
    void LoadTag(int PostId)
    {
        List<TblTermRelationshipsEntity> ls = TblTermRelationshipsManager.CreateInstant().SelectByObjectId(PostId, "posttags");
        rptposttags.DataSource = ls;
        rptposttags.DataBind();
    }
    void LoadViewMore(DateTime Createdate, string TermTaxonomyTextId)
    {
        int TotalRow = 1;
        List<TblPostsEntity> ls = TblPostsManager.CreateInstant().SelectTblPostBy("post", "", -1, TermTaxonomyTextId, "category", "", Createdate, "CreatedDate", "DESC", 10, 1, out TotalRow);
        rptviewmore.DataSource = ls;
        rptviewmore.DataBind();
    }
    public string[] GetTermRelationshipsInfo(object obj)
    {
        List<TblTermRelationshipsEntity> ls = (List<TblTermRelationshipsEntity>)obj;
        string Url = "", TaxonomyName = "";
        if (ls != null && ls.Count > 0)
        {
            Url = UrlBuilder.GetCategory(ls[0].Slug);
            TaxonomyName = ls[0].Name;
        }
        return new string[] { TaxonomyName, Url };
    }

    void SEO(TblPostsEntity item)
    {
        string Title = "", description = "", keywords = "";
        if (item.PostMetas["SEOTitle"] != null && !item.PostMetas["SEOTitle"].Equals(""))
            Title = item.PostMetas["SEOTitle"].ToString() + " - " + TblOptionManager.CreateInstant().GetOption("SEOTitle");
        else Title = item.Title + '-' + TblOptionManager.CreateInstant().GetOption("SEOTitle");
        if (item.PostMetas["SEODescription"] != null && !item.PostMetas["SEODescription"].Equals(""))
            description = item.PostMetas["SEODescription"].ToString();
        else description = TblOptionManager.CreateInstant().GetOption("SEODescription");
        if (item.PostMetas["SEOKeyWord"] != null && !item.PostMetas["SEOKeyWord"].Equals(""))
            keywords = item.PostMetas["SEOKeyWord"].ToString();
        else keywords = TblOptionManager.CreateInstant().GetOption("SEOKeyWord");
        string url = Wp.BLL.TblOptionManager.CreateInstant().GetDomainName() + Request.RawUrl;
        string imageurl = UrlBuilder.GetImage(item.ImageUrl, 100);
        this.Page.Title = Title;
        SetHeaderMetName("description", description);
        SetHeaderMetName("keywords", keywords);

        SetHeaderMetProperty("og:title", Title);
        SetHeaderMetProperty("og:type", "article");
        SetHeaderMetProperty("og:url", url);
        SetHeaderMetProperty("og:image", imageurl);
        SetHeaderMetProperty("og:description", description);
        SetHeaderMetProperty("og:site_name", "Linh Sơn Phật Giáo");
        //SetHeaderMetProperty("fb:admins", "1117161216");


    }
    private void SetHeaderMetName(string Name, string Content)
    {
        HtmlMeta meta = new HtmlMeta();
        meta.Name = Name;
        meta.Content = Content;
        this.Page.Master.FindControl("head").Controls.AddAt(this.Page.Master.FindControl("head").Controls.Count - 1, meta);
    }
    private void SetHeaderMetProperty(string Name, string Content)
    {
        HtmlMeta meta = new HtmlMeta();
        meta.Attributes.Add("property", Name);
        meta.Content = Content;
        this.Page.Master.FindControl("head").Controls.AddAt(this.Page.Master.FindControl("head").Controls.Count - 1, meta);
    }
    public string GetCodeEdit(object id)
    {
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            return "<a href='/admin/Modules/Posts/PostEdit.aspx?Id=" + id + "'  target='_blank'>Edit</a>";
        }
        return "";
    }
    public string GetAvaliableDelete()
    {
        string result = "style='display:none'";
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            result = "style='display:inline'";
        }
        return result;
    }
    void LoadComment(int PostID, int ParentID, string Approved)
    {
        List<TblCommentsEntity> ls = TblCommentsManager.CreateInstant().SelectByPost(PostID, ParentID, Approved, 10000);
        if (ls.Count > 0)
        {
            rptComment.DataSource = ls;
            rptComment.DataBind();
            LiteralCommnent.Text = ls.Count().ToString();
        }

    }
    protected void rptComment_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {

            TblCommentsEntity item = (TblCommentsEntity)e.Item.DataItem;
            Repeater rptchild = (Repeater)e.Item.FindControl("rptchild");
            List<TblCommentsEntity> ls = TblCommentsManager.CreateInstant().SelectByPost(item.PostId, item.Id, "approved");

            if (ls.Count > 0)
            {
                rptchild.DataSource = ls;
                rptchild.DataBind();
            }
        }
    }
    public string StrEdit(object CommentID)
    {
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            return " <a href='/Admin/Modules/Comments/CommentEdit.aspx?Id=" + CommentID + "' target='_blank' >Edit</a>";
        }
        return "";
    }

}